const app = getApp();
import {
  post
} from '../../utils/request';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,

    loginInfo: null,
    config: null,
    analysisData: {
      orderCount: 0,
      orderSum: '0.00',
      userCount: 0
    },

    listResultOrder: {
      list: [],
      total: 0
    },
    listPayloadOrder: {
      pageNum: 1,
      pageSize: 10,
      param: {},
      loading: false,
      hasMore: true,
    },

    listResultUser: {
      list: [],
      total: 0
    },
    listPayloadUser: {
      pageNum: 1,
      pageSize: 10,
      param: {},
      loading: false,
      hasMore: true,
    },

  },

  toInfoPage() {
    let options = app.getPageOptions();
    options = JSON.parse(decodeURIComponent(options.info));
    app.checkLogin(true, () => {
      wx.navigateTo({
        url: `/pages/campain/info?cid=${options.id}&type=${options.aboutType}&aid=${options.aboutId}`,
      });
    });
  },

  onTab(e) {
    let {
      tab
    } = e.currentTarget.dataset;
    this.setData({
      currentTab: tab
    });
    if (tab === 0) {
      this.refreshOrderList();
    }
    if (tab === 1) {
      this.refreshUserList();
    }
  },

  // 获取列表
  getOrderList() {
    let options = app.getPageOptions();
    options = JSON.parse(decodeURIComponent(options.info));
    let that = this;
    if (that.data.listPayloadOrder.loading || (!that.data.listPayloadOrder.hasMore)) {
      return;
    }
    app.checkLogin(true, () => {
      that.setData({
        listPayloadOrder: {
          ...that.data.listPayloadOrder,
          loading: true,
          param: {
            campainId: options.id
          }
        }
      });
      wx.showNavigationBarLoading();
      post("/campain/my-orders", that.data.listPayloadOrder)
        .then(resp => {
          wx.stopPullDownRefresh()
          wx.hideNavigationBarLoading();
          let hasMore = true;
          if (0 == resp.code) {
            let listResult = that.data.listResultOrder;
            let listPayload = that.data.listPayloadOrder;
            let list = (listPayload.pageNum === 1) ? resp.data.list : [...listResult.list, ...resp.data.list];
            list = list.map(d => {
              d.userOrder.actualAmount = (d.userOrder.actualAmount / 100).toFixed(2);
              return d;
            })
            if (resp.data.list.length < listPayload.pageSize) {
              hasMore = false;
            }
            that.setData({
              listPayloadOrder: {
                ...listPayload,
                loading: false,
                hasMore
              },
              listResultOrder: {
                total: resp.data.total,
                list
              }
            });
          } else {
            that.setData({
              listPayloadOrder: {
                ...listPayload,
                loading: false
              }
            });
          }
        })
    });
  },

  // 刷新列表
  refreshOrderList() {
    this.setData({
      listResultOrder: {
        list: [],
        total: 0
      },
      listPayloadOrder: {
        ...this.data.listPayloadOrder,
        pageNum: 1,
        pageSize: 10,
        loading: false,
        hasMore: true,
      }
    });
    this.getOrderList();
  },

  // 获取列表
  getUserList() {
    let options = app.getPageOptions();
    options = JSON.parse(decodeURIComponent(options.info));
    let that = this;
    if (that.data.listPayloadUser.loading || (!that.data.listPayloadUser.hasMore)) {
      return;
    }
    app.checkLogin(true, () => {
      that.setData({
        listPayloadUser: {
          ...that.data.listPayloadUser,
          loading: true,
          param: {
            campainId: options.id
          }
        }
      });
      wx.showNavigationBarLoading();
      post("/campain/my-users", that.data.listPayloadUser)
        .then(resp => {
          wx.stopPullDownRefresh()
          wx.hideNavigationBarLoading();
          let hasMore = true;
          if (0 == resp.code) {
            let listResult = that.data.listResultUser;
            let listPayload = that.data.listPayloadUser;
            let list = (listPayload.pageNum === 1) ? resp.data.list : [...listResult.list, ...resp.data.list];
            if (resp.data.list.length < listPayload.pageSize) {
              hasMore = false;
            }
            that.setData({
              listPayloadUser: {
                ...listPayload,
                loading: false,
                hasMore
              },
              listResultUser: {
                total: resp.data.total,
                list
              }
            });
          } else {
            that.setData({
              listPayloadUser: {
                ...listPayload,
                loading: false
              }
            });
          }
        })
    });
  },

  // 刷新列表
  refreshUserList() {
    this.setData({
      listResultUser: {
        list: [],
        total: 0
      },
      listPayloadUser: {
        ...this.data.listPayloadUser,
        pageNum: 1,
        pageSize: 10,
        loading: false,
        hasMore: true,
      }
    });
    this.getUserList();
  },

  // 统计数据
  getAnalysisData(id) {
    app.checkLogin(true, (loginInfo) => {
      this.setData({
        loginInfo: loginInfo,
      });
      post(`/campain/analysis/${id}`)
        .then(resp => {
          if (resp.code === 0) {
            this.setData({
              analysisData: resp.data
            });
          } else {
            wx.showModal({
              title: '提示',
              content: resp.msg,
              showCancel: false,
            })
          }
        });
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

    if (this.data.currentTab === 0) {
      this.refreshOrderList();
    }

    if (this.data.currentTab === 1) {
      this.refreshUserList();
    }

    wx.setBackgroundColor({
      backgroundColor: "#f6f6f6"
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let options = app.getPageOptions();
    options = JSON.parse(decodeURIComponent(options.info));
    if (options.id) {
      this.getAnalysisData(options.id);
    }

    this.setData({
      loginInfo: app.getLoginInfo(),
    });

    app.getConfig(config => {
      this.setData({
        config
      });
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    if (this.data.currentTab === 0) {
      this.refreshOrderList();
    }

    if (this.data.currentTab === 1) {
      this.refreshUserList();
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.currentTab === 0) {
      if (this.data.listPayloadOrder.loading || (!this.data.listPayloadOrder.hasMore)) {
        return;
      }
      this.setData({
        listPayloadOrder: {
          ...this.data.listPayloadOrder,
          pageNum: this.data.listPayloadOrder.pageNum + 1
        }
      });
      this.getOrderList();
    }

    if (this.data.currentTab === 1) {
      if (this.data.listPayloadUser.loading || (!this.data.listPayloadUser.hasMore)) {
        return;
      }
      this.setData({
        listPayloadUser: {
          ...this.data.listPayloadUser,
          pageNum: this.data.listPayloadUser.pageNum + 1
        }
      });
      this.getUserList();
    }
  },

});